<template>
  <div class="app-container  bg-gray">
    <el-row class="box-1">
      <el-col :span="24">
        <el-card>
          <div class="header">
            <div>
              <el-button type="success" class="bg-green" icon="el-icon-circle-plus" @click="showTypeDialog()">新增分类</el-button>
              <el-alert :closable="false">*注：添加分类后必须添加类型范围才能正确分配人员。</el-alert>
            </div>
          </div>
          <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border highlight-current-row @sort-change="handleSortChange">
            <el-table-column label="分类图标" width="140" header-align="center" align="center">
              <template slot-scope="scope">
                <ws-img :src="scope.row.icon" class="image" alt="" style="width: 60px; height: 60px;"/>
              </template>
            </el-table-column>
            <el-table-column prop="name" label="类型名称（服务范围）" min-width="260" header-align="center" align="center"/>
            <el-table-column prop="formName" label="表单名称" min-width="160" header-align="center" align="center"/>
            <el-table-column prop="sort" label="排序权重" width="120" header-align="center" align="center" sortable/>
            <el-table-column prop="status" label="状态" width="160" header-align="center" align="center">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.status==='1'" type="success">已开启</el-tag>
                <el-tag v-else type="info">已关闭</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="240" header-align="center" align="center">
              <template slot-scope="scope">
                <el-tooltip content="类型范围" placement="top">
                  <el-button size="small" type="primary" class="el-icon-more" circle @click="toServiceArea(scope.row)"/>
                </el-tooltip>
                <el-button type="success" class="bg-green" size="small" icon="el-icon-edit" circle style="margin-left: 10px;" @click="showTypeDialog(scope)"/>
                <el-button type="danger" size="small" icon="el-icon-delete" circle @click="toDelete(scope.row)" />
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :current-page.sync="currentPage"
            :page-size="pageSize"
            :page-sizes="pageSizes"
            :total="total"
            class="pagination"
            layout="total, sizes, prev, pager, next, jumper"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"/>
        </el-card>
      </el-col>
    </el-row>

    <el-dialog
      :visible.sync="typeDialogVisible"
      :title="typeDialogForm.title"
      :modal="false"
      width="750px" class="dialog"
      center
      @close="resetDialog('typeDialogForm')">
      <el-form ref="typeDialogForm" :model="typeDialogForm" :rules="typeDialogFormRules" label-width="140px">
        <el-form-item label="类型名称" prop="name">
          <el-input v-model="typeDialogForm.name"/>
        </el-form-item>
        <el-form-item label="备注说明">
          <el-input v-model="typeDialogForm.remark" type="textarea" rows="5"/>
        </el-form-item>
        <el-form-item label="分类图标" prop="icon">
          <div class="remark">*注:为保证显示效果,请上传80px*80px的图片</div>
          <ws-upload-single-img v-model="typeDialogForm['icon']"
                                :path="typeDialogForm['icon']"
                                width="80" height="80"/>
        </el-form-item>
        <el-form-item label="选择表单" prop="formId">
          <el-select v-model="typeDialogForm['formId']" placeholder="请选择申请表单">
            <el-option v-for="item in formList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="排序权重" prop="sort">
          <el-input-number v-model="typeDialogForm['sort']" :min="0"/>
        </el-form-item>
        <el-form-item label="分类状态" prop="sort">
          <el-radio-group v-model="typeDialogForm.status">
            <el-radio label="1">开启</el-radio>
            <el-radio label="2">关闭</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-divider/>
        <el-form-item label="工单配图" prop="picture">
          <div class="remark">*注:为保证显示效果,请上传750px*400px的图片</div>
          <ws-upload-single-img v-model="typeDialogForm['picture']"
                                :path="typeDialogForm['picture']"
                                width="300" height="160"/>
        </el-form-item>
        <el-form-item label="工单内容介绍" prop="intro">
          <el-input v-model="typeDialogForm.intro" type="textarea" rows="5"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="typeDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitTypeDialog('typeDialogForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { handleSortChange, handleSizeChange, handleCurrentChange } from '@/utils/table';
export default {
  data() {
    return {
      // 表单列表
      formList: [],
      // 添加分类弹出框相关数据
      typeDialogVisible: false,
      typeDialogForm: {},
      typeDialogFormRules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        remark: [{ required: true, message: '请输入备注说明', trigger: 'blur' }],
        icon: [{ required: true, message: '请选择图标', trigger: 'blur' }],
        formId: [{ required: true, message: '请选择表单', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入排序', trigger: 'blur' }],
        status: [{ required: true, message: '请选择状态', trigger: 'blur' }],
        picture: [{ required: true, message: '请选择工单配图', trigger: 'blur' }],
        intro: [{ required: true, message: '请输入工单内容介绍', trigger: 'blur' }]
      },

      // 表格相关数据
      tableMaxHeight: document.documentElement.clientHeight - 280,
      tableLoading: false,
      currentPage: 1,
      pageSize: this.$store.getters.userInfo.pageSize,
      pageSizes: [10, 20, 50, 100, 1000],
      total: 0,
      tableData: []
    }
  },
  created() {
    this.getData();
    this.getAllFormList();
  },

  methods: {
    handleCurrentChange, handleSizeChange, handleSortChange,
    getData(order) {
      this.tableLoading = true
      order || (order = {})
      const url = 'newWork/cat/getPageList'
      const data = {
        pageNo: this.currentPage,
        pageSize: this.pageSize
      }
      order.sortName && (data.sortName = order.sortName)
      order.sortOrder && (data.sortOrder = order.sortOrder)
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.tableData = res.data.rows
        this.total = res.data.total
        this.tableLoading = false
      }).catch(e => {
        this.tableLoading = false
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    // 获取所有表单
    getAllFormList() {
      const url = 'form/getAll';
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.formList = res.data;
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    toServiceArea(row) {
      this.isShow = false
      this.$router.push({
        path: '/superWorkOrder/serviceArea',
        query: { id: row.id }
      })
    },

    toDelete(row) {
      this.$confirm('此操作将删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = 'newWork/cat/delete'
        const data = { id: row.id };
        this.$store.dispatch('GetConnect', { url, data }).then(res => {
          this.$message.success(res.msg)
          this.getData()
        }).catch(e => {
          this.$message.error(e.msg);
        })
      })
    },

    // 显示分类弹窗
    showTypeDialog(scope) {
      if (scope) {
        this.typeDialogForm = Object.assign({}, scope.row)
        this.typeDialogForm.title = '编辑分类'
      } else {
        this.typeDialogForm = {};
        this.typeDialogForm.title = '新增分类';
      }
      this.typeDialogVisible = true;
    },

    /**
     * 弹窗表单提交方法,通过判断有没有form_id执行添加或编辑操作
     * formName: form表单中ref的值,即表单名
     */
    submitTypeDialog(formName) {
      this.$refs[formName].validate((valid) => { // 表单验证
        if (valid) {
          const url = this.typeDialogForm.id ? 'newWork/cat/update' : 'newWork/cat/create';
          const data = this.typeDialogForm;
          this.$store.dispatch('GetConnect', { url, data }).then(res => {
            this.typeDialogVisible = false;
            this.$message.success(res.msg);
            this.getData();
          }).catch(e => {
            this.$message.error(e.msg + ',请刷新或联系管理员!')
          })
        } else {
          this.$message.error('提交失败,请检查必填项')
        }
      })
    },
    resetDialog(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../../styles/index.scss';
.box-1 {
  .header {
    @include flex-row(center, space-between);
    padding-bottom: 16px;
    flex-wrap: wrap;
    > div {
      @include flex-row(center, flex-start);
      flex-wrap: wrap;
      width: 100%;
    }
    .search-input {
      width: 240px;
    }
    .do-btn {
      @extend .bg-green;
      margin-left: 6px;
    }
  }
}
.el-tag {
  cursor: pointer;
}
</style>
